package com.tech.koufu.ui.adapter;

import java.util.ArrayList;
import java.util.zip.Inflater;

import com.tech.koufu.R;
import com.tech.koufu.model.EntrustStock;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.activity.BuyStocksActivity; 

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 
 * @ClassName: TradeAdapter
 * @Description: 我的交易适配器 
 * @author jl_luo
 * @date 2015年4月4日 下午1:39:01
 */
public class TradeAdapter extends BaseAdapter {

	private Context context;  
	private LayoutInflater mInflater = null; 
	public ArrayList<EntrustStock> datas = null;
//	public ArrayList<UserStocks> target = null;
	
	public TradeAdapter(Context context) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context); 
	}
	@Override
	public int getCount() { 
		if(null!=datas){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		return null;
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_trade, parent,false); 
              
             holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);  
             holder.txt_stock_time = (TextView)convertView.findViewById(R.id.txt_stock_time);   
             holder.txt_buy_price = (TextView)convertView.findViewById(R.id.txt_buy_price);
             holder.txt_buy_num = (TextView)convertView.findViewById(R.id.txt_buy_num);
             holder.txt_stocks_remain = (TextView)convertView.findViewById(R.id.txt_stocks_remain);
             holder.txt_poundage = (TextView)convertView.findViewById(R.id.txt_poundage);
             holder.txt_status = (TextView)convertView.findViewById(R.id.txt_status);
             
             holder.btn_buy_stock = (Button)convertView.findViewById(R.id.btn_buy);
             holder.btn_sell_stock = (Button)convertView.findViewById(R.id.btn_sell);
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
       final  EntrustStock userStocks = datas.get(position);
       	 holder.txt_stock_time .setText(userStocks.time);
         String name = userStocks.getStockName();String code = userStocks.getStockId();
         String stock_name = String.format(context.getResources().getString(R.string.txt_stock_name_code), name,code);
         SpannableStringBuilder ssb =  new SpannableStringBuilder(stock_name);
         ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.TextColorRed_FD0000)),name.length()+1,stock_name.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         holder.txt_stock_name.setText(ssb.toString());
          
         if(userStocks.entrustBs.equals("r")){
             SpannableStringBuilder ssb1 =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_input_buy_stock_price), userStocks.price));
             ssb1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,userStocks.price.length() +5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
             holder.txt_buy_price.setText(ssb1);
              
             ssb =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_input_buy_stock_num), userStocks.amount));
             ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,userStocks.amount.length() + 5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
             holder.txt_buy_num.setText(ssb);
         }else{
             SpannableStringBuilder ssb1 =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_input_sell_stock_price), userStocks.price));
             ssb1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,userStocks.price.length() +5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
             holder.txt_buy_price.setText(ssb1);
              
             ssb =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_input_sell_stock_num), userStocks.amount));
             ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,userStocks.amount.length() + 5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
             holder.txt_buy_num.setText(ssb);
         }

         
         float bj =  Float.parseFloat(userStocks.price);
         int num = Integer.parseInt(userStocks.amount);
         ssb =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_stocks_remain_num), KouFuTools.df2.format(bj*num)));
         ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         holder.txt_stocks_remain.setText(ssb);
           
         //成交价格 *成交数量 *佣金比例
         float sxf = 0f;
         if(userStocks.entrustBs.equals("r")){//mairu
        	 sxf = bj*num*0.001f;
        	 
         }else{
        	 sxf = bj*num*0.002f;
         } 
         holder.txt_poundage.setText(String.format(context.getResources().getString(R.string.txt_poundage), KouFuTools.df2.format(sxf)));
         holder.txt_status.setText(String.format(context.getResources().getString(R.string.txt_entrust_status_s), userStocks.status));
        
        holder.btn_buy_stock.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) { 
				Intent intent = new Intent(context,BuyStocksActivity.class); 
				intent.putExtra("stockInfo", userStocks);  
				intent.putExtra("datatype", 0);
				intent.putExtra("type", 0);
				intent.putExtra("userStocks", datas);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
        
        holder.btn_sell_stock.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,BuyStocksActivity.class); 
				intent.putExtra("stockInfo", userStocks);
				intent.putExtra("datatype", 0);
				intent.putExtra("type", 1);
				intent.putExtra("userStocks", datas);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
        
         return convertView;
	}
	
	static class ViewHolder  
    {   
		 
        public TextView txt_stock_name;  
        public TextView txt_stock_time;   
        public TextView txt_buy_price;
        public TextView txt_buy_num;
        public TextView txt_stocks_remain;
        public TextView txt_poundage;
        public Button btn_buy_stock;
        public Button btn_sell_stock;
        public TextView txt_status;
        
    }  

}
